<template>
	<gui-page ref="guipage">
		<view slot="gBody">
			<view class="box">
				<view class="boxItem">
					<input type="text" class="gui-form-input" v-model="newData" @input="inputting"
						placeholder="请输入内容" />
					<text class="gui-form-icon gui-icons gui-text-center gui-color-gray" v-if="newData.length >= 1"
						@click="clearName2">&#xe632;</text>
				</view>
			</view>
			<view class="saveBtn" @tap="submit">确认</view>
		</view>
	</gui-page>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				titleText: '昵称',
				oldData: '',
				newData: '',
				type: 1,
				typeName: ''
			}
		},
		onLoad(options) {
			console.log(options);
			this.type = parseInt(options.type)
			this.typeName = this.type == 1 ? '昵称' : this.type == 2 ? '真实姓名' : this.type == 3 ? '客户号' : this.type == 4 ?
				'邮箱' : '微信号'
			if (options.data && options.data != 'null') {
				this.newData = options.data
			}
			this.oldData = options.data
		},
		methods: {
			// 监听输入框输入事件
			inputting: function(e) {
				this.newData = e.detail.value;
			},
			// 清空输入框的值
			clearName2: function() {
				this.newData = '';
			},
			submit() {
				let formData = {}
				if (!this.newData) {
					this.$u.msg('请输入' + this.typeName)
					return;
				}
				if (this.newData == this.oldData) {
					this.$u.msg(this.typeName + '未更换哦')
					return;
				}
				if (this.type == 1) {
					formData.username = this.newData
				} else if (this.type == 2) {
					formData.realName = this.newData
				} else if (this.type == 3) {
					formData.customerNum = this.newData
				} else if (this.type == 4) {
					formData.email = this.newData
				} else if (this.type == 5) {
					formData.wechat = this.newData
				}
				this.$http.post('/api/oauth/edit', formData).then(res => {
					console.log(res)
					this.$u.msg('修改成功！')
					if (this.type == 1) {
						this.vuex_userInfo.username = formData.username
					} else if (this.type == 2) {
						this.vuex_userInfo.realName = formData.realName
					} else if (this.type == 3) {
						this.vuex_userInfo.customerNum = formData.customerNum
					} else if (this.type == 4) {
						this.vuex_userInfo.email = formData.email
					} else if (this.type == 5) {
						this.vuex_userInfo.wechat = formData.wechat
					}
					this.$u.vuex('vuex_userInfo', this.vuex_userInfo);
					this.$u.back(1, 1000)

				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background: #F6F7F8;
	}

	.box {
		padding-top: 20rpx;

		.boxItem {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #FFFFFF;
			padding: 30rpx;
		}
	}

	.saveBtn {
		width: 513rpx;
		height: 80rpx;
		margin: 0 auto;
		margin-top: 50rpx;
	}
</style>